package com.lbd.smsplatform.service.impl;

import com.lbd.base.service.BaseServiceImpl;
import com.lbd.smsplatform.bean.SmsTemplate;
import com.lbd.smsplatform.dao.SmsTemplateRepository;
import com.lbd.smsplatform.service.SmsTemplateService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by wang on 2016/9/23.
 */
@Service
@Transactional
public class SmsTemplateServiceImpl extends BaseServiceImpl<SmsTemplate, SmsTemplateRepository> implements SmsTemplateService {

    private final Logger log = Logger.getLogger(this.getClass());
    @Autowired
    SmsTemplateRepository smsTemplateRepository;


    /**
     * 构建短信模板信息的查询条件和参数
     *
     * @param smsTemplate
     * @param parameter
     * @return
     */
    @Override
    public String buildJpql(SmsTemplate smsTemplate, Map<String, Object> parameter) {
        StringBuilder jpqlBuilder = new StringBuilder("  from  SmsTemplate t   ");
        if (smsTemplate != null) {
            if (StringUtils.isNotBlank(smsTemplate.getCode())) {
                jpqlBuilder.append(" and  code = :code");
                parameter.put("code", smsTemplate.getCode());
            }
        }
        jpqlBuilder.append(" order by code ");
        return jpqlBuilder.toString();
    }

    /**
     * 根据ID查询短信模板信息
     *
     * @param id
     * @return
     */
    @Override
    public SmsTemplate findById(String id) {
        return smsTemplateRepository.findOne(id);
    }
}
